package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.DrugListAdapter;
import com.hxkcloud.nurse.beans.DrugBean;
import com.hxkcloud.nurse.beans.DrugsList;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-10-08.
 * Description:药品列表
 */
public class DrugListActivity extends BaseActivity implements View.OnClickListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private RecyclerView rv;
    private Button bt_next;

    private List<DrugBean> drugs;
    private DrugListAdapter adapter;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_DRUGSLIST_SUCCESS:
                drugs = (List<DrugBean>) msg.obj;
                adapter.setData(drugs);
                break;
            case MessageCode.NETWORKCODE_DRUGSLIST_FAILURE:
                if ((msg.obj != null) && (!TextUtils.isEmpty((String) msg.obj))) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_druglist);

        initView();
        initData();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.takedrugrecord));
        iv_back.setOnClickListener(this);

        rv = (RecyclerView) findViewById(R.id.rv_druglist);
        bt_next = (Button) findViewById(R.id.bt_druglist_next);

        bt_next.setOnClickListener(this);

        bt_next.setBackgroundResource(R.drawable.bt_grey);
        bt_next.setEnabled(false);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(mLayoutManager);

        drugs = new ArrayList<>();
        adapter = new DrugListAdapter(mContext, drugs, new DrugListAdapter.onDrugCheckListener() {
            @Override
            public void onDrugCheck(int position) {
                drugs.get(position).setIsCheck(!drugs.get(position).isCheck());
                adapter.setData(drugs);

                boolean isSelect = false;
                for (DrugBean drug : drugs) {
                    if (drug.isCheck()) {
                        isSelect = true;
                        break;
                    }
                }
                if (isSelect) {
                    bt_next.setBackgroundResource(R.drawable.bt_green);
                    bt_next.setEnabled(true);
                } else {
                    bt_next.setBackgroundResource(R.drawable.bt_grey);
                    bt_next.setEnabled(false);
                }
            }
        });
        rv.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.drugsList(loginInfo.getDepartmentId(), handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.bt_druglist_next:
                DrugsList drugsList = new DrugsList();
                List<DrugBean> drugBeans = new ArrayList<>();
                for (DrugBean drug : drugs) {
                    if (drug.isCheck()) {
                        drugBeans.add(drug);
                    }
                }
                if (drugBeans.size() == 0) {
                    toastMsg(R.string.selectleastonedrug);
                    return;
                }
                drugsList.setList(drugBeans);

                Intent iAdd = new Intent(mContext, AddDrugActivity.class);
                iAdd.putExtra("druglist", (Serializable) drugsList);
                startActivity(iAdd);
                finish();
                break;
        }
    }
}
